package cn.sdormitory.controller;

import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.common.exception.ApiException;
import cn.sdormitory.smartdor.vo.SdAttenceReportVo;
import cn.sdormitory.workflow.entity.StudentsViolationApply;
import cn.sdormitory.workflow.entity.ViolationApply;
import cn.sdormitory.workflow.service.NotRetrunedApplyService;
import cn.sdormitory.workflow.service.ViolationApplyService;
import cn.sdormitory.workflow.service.WorkFlowCommonService;
import cn.sdormitory.workflow.vo.NotRetrunedApplyVO;
import cn.sdormitory.workflow.vo.WorkFlowVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/violation")
public class AppViolationApplyController {
    @Resource
    ViolationApplyService violationApplyService;

    @Resource
    WorkFlowCommonService workFlowCommonService;
    /**
     * 提交流程
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/createApply")
    public CommonResult<Integer> createApply(@RequestBody List<StudentsViolationApply> applyList){
        int apply = violationApplyService.createApply(applyList);
        if(apply>0){
            return CommonResult.success();
        }
        return CommonResult.failed();

    }

    /**
     * 查询申请单
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/pageList")
    public CommonResult<CommonPage<ViolationApply>> pageList(@RequestBody ViolationApply vo){
        IPage<ViolationApply> page = violationApplyService.pageList(vo);
        return CommonResult.success(CommonPage.restPage(page));
    }
    
    /**
     * 查询已处理申请单
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/getProcessedPageList")
    public CommonResult<CommonPage<ViolationApply>> getProcessedPageList(@RequestBody ViolationApply vo){
        IPage<ViolationApply> page = violationApplyService.getProcessedPageList(vo);
        return CommonResult.success(CommonPage.restPage(page));
    }

    /**
     * 撤销申请单
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/cancelApply")
    public CommonResult cancelApply(@RequestBody ViolationApply vo){
        boolean b = violationApplyService.revokeApply(vo);
        if(b){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 查询申请单详情
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/detail")
    public CommonResult<ViolationApply> applyDetail(@RequestBody ViolationApply vo){
        return CommonResult.success(violationApplyService.applyDetail(vo));
    }

    /**
     * 已审批申请单
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/findHistoryApproval")
    public CommonResult<CommonPage<ViolationApply>> findHistoryApproval(@RequestBody WorkFlowVO vo) throws ApiException{
        IPage<ViolationApply> list = violationApplyService.findHistoryApproval(vo);
        return CommonResult.success(CommonPage.restPage(list));
    }

    /**
     * 流程审批
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/processWorkFlow")
    public CommonResult<WorkFlowVO> processWorkFlow(@RequestBody WorkFlowVO vo) throws ApiException{
        workFlowCommonService.processWorkFlow(vo);
        return CommonResult.success();
    }

    /**
     * 获取流程信息
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/workFlowInfo")
    public CommonResult<WorkFlowVO> workFlowInfo(@RequestBody WorkFlowVO vo) throws ApiException{
        WorkFlowVO workFlowVO = workFlowCommonService.workFlowInfo(vo);
        if(workFlowVO!=null){
            return CommonResult.success(workFlowVO);
        }
        return CommonResult.failed();
    }
    
    @ApiOperation(value = "违纪统计")
    @PostMapping("/getStuViolationStatistics")
    public CommonResult getStuViolationStatistics(@RequestBody Map<String, String> param){
        String startDate = param.get("startDate");
        String endDate = param.get("endDate");
        List<Map<String, String>> resultList = violationApplyService.getViolationStatistics(startDate, endDate);
        return CommonResult.success(resultList);
    }
    @ApiOperation(value = "违纪详情")
    @PostMapping("/getStuViolationStatisticsDetail")
    public CommonResult getStuViolationStatisticsDetail(@RequestBody Map<String, String> param){
        String startDate = param.get("startDate");
        String endDate = param.get("endDate");
        String sdId = param.get("sdId");
        List<ViolationApply> resultList = violationApplyService.getViolationStatisticsDetail(startDate, endDate, sdId);
        return CommonResult.success(resultList);
    }
    
}
